package com.lbs.service.Impl;

import com.lbs.dao.Impl.UserDaoImpl;
import com.lbs.dao.UserDao;
import com.lbs.entity.User;
import com.lbs.service.UserService;
import com.lbs.utils.PageUtils;


import java.util.List;

/**
 * @author Administrator
 * @date 2021/5/29 13:58
 * @description
 **/
public class UserServiceImpl implements UserService {

    UserDao userDao = new UserDaoImpl();

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User login(String username, String password) {
        return userDao.findByUsernameAndPassword(username, password);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public boolean add(User user) {
        return userDao.insertUser(user) > 0;
    }

    /**
     * 分页查询
     * @param search        查询用户的条件
     * @param pageSize      分页显示的数量
     * @param currentPage   当前页
     * @return
     */
    @Override
    public PageUtils<User> findUserByName(String search, int pageSize, int currentPage) {

        StringBuilder builder = new StringBuilder();

        String sql = "select * from user where name like ?";

        builder.append(sql);

        int total = userDao.selectAllByName(builder.toString(), "%" + search + "%").size();

        builder.append(" limit ?,?");

        List<User> users = userDao.selectAllByName(builder.toString(), "%" + search + "%", (currentPage - 1) * pageSize, pageSize);

        PageUtils<User> PageUtils = new PageUtils<>(pageSize, currentPage, total, users);

        return PageUtils;
    }

    /**
     * 批量删除
     * @param uids
     * @return
     */
    @Override
    public boolean batchDel(String[] uids) {
        try {
            for (String uid : uids){
                int i = userDao.deleteUserById(uid);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public boolean updateUser(User user) {
        return userDao.updateUserById(user)>0;
    }
}
